package com.mycompany.droolsexample;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

public class App {

    public static void main(String[] args) {
        App controller = new App();
        controller.foo();
    }

    public void foo() {
        KnowledgeBase kbase = getKnowledgeBase();

        StatefulKnowledgeSession ksStatefull = kbase.newStatefulKnowledgeSession();

        Message m1 = new Message();
        m1.setType("Are you there, Drools?");

        Message m2 = new Message();
        m2.setType("How are your, Drools?");

        ksStatefull.insert(m1);
        ksStatefull.insert(m2);
        ksStatefull.fireAllRules();

        Message m3 = new Message();
        m3.setMsgtext("Hello World");
        m3.setStatus(Message.HELLO);
        ksStatefull.insert(m3);
        ksStatefull.fireAllRules();
        System.out.println(m3.getMsgtext());

        ksStatefull.dispose();
    }

    
    public KnowledgeBase getKnowledgeBase() {

        final String DRL_FILE = "testrules.drl";

// Construct a KnowledgeBuilder from the KnowledgeBuilderFactory
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

// Add to the KnowledgeBuilder a file rules.drl from the classpath
        kbuilder.add(ResourceFactory.newClassPathResource(DRL_FILE, getClass()), ResourceType.DRL);

// Check for errors and print them if necessary
        if (kbuilder.hasErrors()) {
            System.out.println(kbuilder.getErrors().toString());
        }
// Construct the KnowledgeBase from the KnowledgeBaseFactory
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
// add the compiled package to the KnowledgeBase
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

        return kbase;

    }
}
